package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.WmsInventoryCheck;
import com.ruoyi.system.domain.WmsInventoryHistory;
import com.ruoyi.system.enums.OperationType;
import com.ruoyi.system.enums.WmsInventoryCheckStatus;
import com.ruoyi.system.mapper.WmsInventoryCheckMapper;
import com.ruoyi.system.mapper.WmsInventoryHistoryMapper;
import com.ruoyi.system.service.IWmsInventoryCheckService;
import com.ruoyi.system.utils.GetOrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 盘库信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-01
 */
@Service
public class WmsInventoryCheckServiceImpl implements IWmsInventoryCheckService
{
    @Autowired
    private WmsInventoryCheckMapper wmsInventoryCheckMapper;
    @Autowired
    private GetOrderNoUtil getOrderNoUtil;

    @Autowired
    private WmsInventoryHistoryMapper wmsInventoryHistoryMapper;

    /**
     * 查询盘库信息
     *
     * @param id 盘库信息主键
     * @return 盘库信息
     */
    @Override
    public WmsInventoryCheck selectWmsInventoryCheckById(Long id)
    {
        return wmsInventoryCheckMapper.selectWmsInventoryCheckById(id);
    }

    /**
     * 查询盘库信息列表
     *
     * @param wmsInventoryCheck 盘库信息
     * @return 盘库信息
     */
    @Override
    public TableDataInfo<WmsInventoryCheck> selectWmsInventoryCheckList(WmsInventoryCheck wmsInventoryCheck, PageQuery pageQuery)
    {
        Page<WmsInventoryCheck> page = wmsInventoryCheckMapper.selectWmsInventoryCheckList(pageQuery.build(),wmsInventoryCheck);
        return TableDataInfo.build(page);
    }

    /**
     * 新增盘库信息
     *
     * @param wmsInventoryCheck 盘库信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertWmsInventoryCheck(WmsInventoryCheck wmsInventoryCheck)
    {

        //新增出库信息
        if(WmsInventoryCheckStatus.COMPLETED.getCode().equals(wmsInventoryCheck.getInventoryCheckStatus())){
            wmsInventoryCheck.setCreateTime(new Date());
            wmsInventoryCheck.setInventoryCheckUserId(LoginHelper.getUserId());
        }

        int i = wmsInventoryCheckMapper.insertWmsInventoryCheck(wmsInventoryCheck);
        //插入二维码(待定)

        for (WmsInventoryHistory wmsInventoryHistory : wmsInventoryCheck.getInventoryHistoryList()){

            wmsInventoryHistory.setInboundId(wmsInventoryHistory.getId());


            wmsInventoryHistory.setOperationId(wmsInventoryCheck.getId());
            wmsInventoryHistory.setOperationNo(wmsInventoryCheck.getInventoryCheckOrderNo());
            wmsInventoryHistory.setOperationType(OperationType.INVENTORY.getCode());
            wmsInventoryHistory.setCreateTime(new Date());
            wmsInventoryHistory.setId(null);
            wmsInventoryHistoryMapper.insertWmsInventoryHistory(wmsInventoryHistory);

        }
        return i;
    }

    /**
     * 修改盘库信息
     *
     * @param wmsInventoryCheck 盘库信息
     * @return 结果
     */
    @Override
    public int updateWmsInventoryCheck(WmsInventoryCheck wmsInventoryCheck)
    {
        //新增出库信息
        if(WmsInventoryCheckStatus.COMPLETED.getCode().equals(wmsInventoryCheck.getInventoryCheckStatus())){
            wmsInventoryCheck.setCreateTime(new Date());
            wmsInventoryCheck.setInventoryCheckUserId(LoginHelper.getUserId());
        }
        wmsInventoryCheck.setUpdateTime(DateUtils.getNowDate());
        int i = wmsInventoryCheckMapper.updateWmsInventoryCheck(wmsInventoryCheck);
        //插入二维码(待定)

        WmsInventoryHistory wmsInventoryHistory = new WmsInventoryHistory();
        wmsInventoryHistory.setOperationId(wmsInventoryCheck.getId());

        List<WmsInventoryHistory> wmsInventoryHistorieList = wmsInventoryHistoryMapper.selectWmsInventoryHistoryDetailListFromOutbound(wmsInventoryHistory);


        //查询老的数据的inboundId


        Long[] ids = wmsInventoryHistorieList.stream().map(WmsInventoryHistory::getId)  // 提取每个对象的id
                .toArray(Long[]::new);           // 转换为Long数组


        //删除之前的商品明细
        wmsInventoryHistoryMapper.deleteWmsInventoryHistoryByIds(ids);

        for (WmsInventoryHistory wmsInventoryHistoryDetail : wmsInventoryCheck.getInventoryHistoryList()){

//            wmsInventoryHistoryDetail.setInboundId(wmsInventoryHistory.getId());


            wmsInventoryHistoryDetail.setOperationId(wmsInventoryCheck.getId());
            wmsInventoryHistoryDetail.setOperationNo(wmsInventoryCheck.getInventoryCheckOrderNo());
            wmsInventoryHistoryDetail.setOperationType(OperationType.INVENTORY.getCode());
            wmsInventoryHistoryDetail.setCreateTime(new Date());
            wmsInventoryHistoryDetail.setId(null);
            wmsInventoryHistoryMapper.insertWmsInventoryHistory(wmsInventoryHistoryDetail);

        }
        return i;
    }

    /**
     * 批量删除盘库信息
     *
     * @param ids 需要删除的盘库信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsInventoryCheckByIds(Long[] ids)
    {
        return wmsInventoryCheckMapper.deleteWmsInventoryCheckByIds(ids);
    }

    @Override
    public int cancellation(Long id){
        WmsInventoryCheck wmsInventoryCheck = new WmsInventoryCheck();
        wmsInventoryCheck.setInventoryCheckStatus(WmsInventoryCheckStatus.CANCELED.getCode());
        wmsInventoryCheck.setId(id);
        wmsInventoryCheck.setInventoryCheckUserId(LoginHelper.getUserId());
        wmsInventoryCheck.setCreateTime(new Date());
        return wmsInventoryCheckMapper.updateWmsInventoryCheck(wmsInventoryCheck);
    }

    /**
     * 删除盘库信息信息
     *
     * @param id 盘库信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsInventoryCheckById(Long id)
    {
        return wmsInventoryCheckMapper.deleteWmsInventoryCheckById(id);
    }

    @Override
    public WmsInventoryCheck getInventoryCheckOrderNo(){
        WmsInventoryCheck wmsInventoryCheck = new WmsInventoryCheck();
        wmsInventoryCheck.setInventoryCheckOrderNo(getOrderNoUtil.getOrderNo("PK-"));

        return wmsInventoryCheck;
    }
}
